<?php
class HostsController extends AppController {
	var $name = 'Hosts';   
        //name

        var $components = array('Acl');

        var $helpers = array('Html', 'Form', 'Javascript');

	function register() {
		if (!empty($this->data)) {
			$this->Host->create();

                        if (!empty($this->data['Host']['passwd']) && !empty($this->data['Host']['password'])) {
                            if ($this->data['Host']['password'] != $this->data['Host']['passwd']) {
                                $this->Host->invalidate('pass_wrong');
                            }
                        } else {
                             $this->Host->invalidate('empty_pass');
                        }

                        $this->data['Host']['passwd'] = md5($this->data['Host']['passwd']);
                        
			if ($this->Host->save($this->data)) {
				$this->Session->setFlash(__('The host has been saved', true));

                                $id = $this->Host->getLastInsertId();

                                //Moving and renaming the file uploaded
                                if($this->data['Host']['image']){
                                    $target_path = 'img/profile/host/'.$id.'.jpg';
                                    move_uploaded_file($this->data['Host']['image']['tmp_name'], $target_path);
                                }

                                //Creating ARO for the new host
                                $parent = $this->Acl->Aro->findByAlias('Hosts');
                                $aro = new Aro();
                                $aro->create();

                                $aro->save(array(
                                'alias' => $this->data['Host']['email_addr'],
                                'model' => 'Host',
                                'foreign_key' => $this->Host->id,
                                'parent_id' => $parent['Aro']['id'])
                                );

                                $this->Acl->Aro->save();

                                //Creating ACO group for the new host
                                $parent = NULL;
                                $this->Acl->Aco->create(array(
                                    'alias' => $this->data['Host']['email_addr'],
                                    'model' => 'Host',
                                    'foreign_key' => $this->Host->id,
                                    'parent_id' => $parent)
                                    );
                                $this->Acl->Aco->save();

				$this->redirect(array('action' => 'login'));
                                //$this->Session->write('host', $this->data['Host']['email_addr']);
                              
			} else {
                            $this->data['Host']['passwd'] = '';
                        }
		}
	}


	function edit($id = null) {

            if (!$id && empty($this->data)) {
                $this->Session->setFlash(__('Invalid host', true));
                $this->redirect(array('action' => 'index'));
            }

            $currentHost = $this->Session->read('host');
            $this->set('host', $currentHost);

            if (!empty($this->data)) {


                if (!empty($this->data['Host']['old_password']) && !empty($this->data['Host']['new_password'])) {
                    $this->set('mypassold', md5($this->data['Host']['old_password']));
                    $this->Session->setFlash('A match!');

                    if (md5($this->data['Host']['old_password']) == $currentHost['passwd']) {


                        $this->data['Host']['passwd'] = md5($this->data['Host']['new_password']);
                        if ($this->Host->save($this->data)) {

                            //If email changes --> Updating ACO & ARO
                            if ($currentHost['email_addr'] != $this->data['Host']['email_addr']) {

                                $aro = $this->Acl->Aro->findByAlias($currentHost['email_addr']);

                                $aro_id = $aro['Aro']['id'];

                                $query = 'update aros set alias = \'' . $this->data['Host']['email_addr'] . '\' where id = ' . $aro_id;

                                $this->Host->query($query);

                                $aco = $this->Acl->Aco->findByAlias($currentHost['email_addr']);

                                $aco_id = $aco['Aco']['id'];

                                $query = 'update acos set alias = \'' . $this->data['Host']['email_addr'] . '\' where id = ' . $aco_id;

                                $this->Host->query($query);
                            }

                            $this->Session->delete('host');
                            $host = $this->Host->read(null, $id);
                            $this->Session->write('host', $host['Host']);
                            $this->Session->setFlash(__('The host\'s information has been saved', true));
                            //Moving and renaming the file uploaded
                            if ($this->data['Host']['image']) {
                                $target_path = 'img/profile/host/' . $id . '.jpg';
                                move_uploaded_file($this->data['Host']['image']['tmp_name'], $target_path);
                            }

                            $this->redirect(array('action' => 'index'));
                        } else {
                            $this->Session->setFlash("There's something wrong here!");

                            $this->Session->setFlash(__('The host\'s information could not be saved. Please, try again.', true));
                        }
                    }
                } else {
                    if (md5($this->data['Host']['old_password']) == $currentHost['passwd']) {
                        if ($this->Host->save($this->data)) {

                            //If email changes --> Updating ACO & ARO
                            if ($currentHost['email_addr'] != $this->data['Host']['email_addr']) {

                                $aro = $this->Acl->Aro->findByAlias($currentHost['email_addr']);

                                $aro_id = $aro['Aro']['id'];

                                $query = 'update aros set alias = \'' . $this->data['Host']['email_addr'] . '\' where id = ' . $aro_id;

                                $this->Host->query($query);

                                $aco = $this->Acl->Aco->findByAlias($currentHost['email_addr']);

                                $aco_id = $aco['Aco']['id'];

                                $query = 'update acos set alias = \'' . $this->data['Host']['email_addr'] . '\' where id = ' . $aco_id;

                                $this->Host->query($query);
                            }

                            $this->Session->delete('host');
                            //$this->Session->write('host', $this->data['Host']);
                            $this->Session->setFlash(__('The host\'s information has been saved', true));

                            $host = $this->Host->read(null, $id);
                            $this->Session->write('host', $host['Host']);

                            //Moving and renaming the file uploaded
                            if ($this->data['Host']['image']) {
                                $target_path = 'img/profile/host/' . $id . '.jpg';
                                move_uploaded_file($this->data['Host']['image']['tmp_name'], $target_path);
                                $this->redirect(array('action' => 'index'));
                            }
                        } else {
                            $this->Session->setFlash('Data was not saved!');
                        }
                    } else {
                        //$this->Session->setFlash(__("New password invalid.", true));
                        $this->Session->setFlash(__('The password is invalid', true));
                        $this->redirect(array('action' => 'edit', $id));
                    }
                }
            }

            if (empty($this->data)) {
                $this->data = $this->Host->read(null, $id);
                //
            }
	}
        function login(){
            if($this->data){
                $result = $this->Host->find('first', array('conditions'=>array('Host.email_addr'=>$this->data['Host']['email_addr'])));
                $this->set('result', $result);

                if($result && ($result['Host']['passwd'] == md5($this->data['Host']['passwd']))){
                    $this->Session->write('host',$result['Host']);                   
                    $this->Session->write('account_type', 'host');
                    $this->redirect(array('action'=>'index'), null, true);
                    $this->redirect(array('controller' => 'events', 'action' => 'index'), null, true);
                } else {
                    $this->set('error', true);
                }
            }
        }
        function logout(){
            $this->Session->delete('host');          
            $this->Session->delete('account_type');
            $this->redirect(array('action'=>'login'), null, true);
        }
        function index(){
            $host = $this->Session->read('host');
            //$this->set('myemail', $email_addr);
            //$host_first_name  = $host['first_name'];
            if($host){
                //$result = $this->Host->find('first', array('conditions'=>array('Host.email_addr'=>$email_addr)));
                //$this->set('myhost', $host);
                $this->set('host', $host);
            } else {
                $this->redirect(array('action'=>'login'), null, true);
            }
        }
        function vevent(){
            $myhost = $this->Session->read('host');
            $myevent = $this->Host->find('all', array('conditions'=>array('Host.id'=>$myhost['id'])));
            $this->set('test', $myevent[0]['Event']);
        }
	function test(){
           
            
        }
}
?>